#!/usr/bin/env python3
"""
DataPlexus 数据源管理最终集成验证脚本
运行所有验证测试并生成最终报告
"""

import sys
import os
import asyncio
import subprocess
from datetime import datetime
from typing import Dict, Any, List

# 添加项目根目录到Python路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from app.core.logger import logger


class FinalIntegrationTest:
    """最终集成测试类"""
    
    def __init__(self):
        self.test_results = {}
        self.script_dir = os.path.dirname(os.path.abspath(__file__))
        self.project_root = os.path.dirname(self.script_dir)
    
    async def run_backend_verification(self) -> Dict[str, Any]:
        """运行后端验证测试"""
        logger.info("运行后端集成验证...")
        
        try:
            # 运行数据源集成验证
            from verify_datasource_integration import DataSourceIntegrationVerifier
            
            verifier = DataSourceIntegrationVerifier()
            integration_results = await verifier.run_verification()
            
            return {
                'success': integration_results['overall_success'],
                'details': integration_results,
                'test_type': 'backend_integration'
            }
            
        except Exception as e:
            logger.error(f"后端验证失败: {str(e)}")
            return {
                'success': False,
                'error': str(e),
                'test_type': 'backend_integration'
            }
    
    async def run_performance_verification(self) -> Dict[str, Any]:
        """运行性能验证测试"""
        logger.info("运行性能验证...")
        
        try:
            from performance_verification import PerformanceVerifier
            
            verifier = PerformanceVerifier()
            performance_results = await verifier.run_all_performance_tests()
            
            return {
                'success': performance_results['overall_success'],
                'details': performance_results,
                'test_type': 'performance'
            }
            
        except Exception as e:
            logger.error(f"性能验证失败: {str(e)}")
            return {
                'success': False,
                'error': str(e),
                'test_type': 'performance'
            }
    
    def run_database_setup_verification(self) -> Dict[str, Any]:
        """验证数据库设置"""
        logger.info("验证数据库设置...")
        
        try:
            # 检查数据库表是否存在
            from app.core.database import get_sync_engine
            from sqlalchemy import text
            
            engine = get_sync_engine()
            
            with engine.connect() as conn:
                # 检查数据源相关表
                tables_to_check = [
                    'dp_datasources',
                    'dp_datasource_test_logs',
                    'dp_datasource_usage_stats'
                ]
                
                existing_tables = []
                missing_tables = []
                
                for table in tables_to_check:
                    result = conn.execute(text(f"""
                        SELECT COUNT(*) as count 
                        FROM information_schema.tables 
                        WHERE table_schema = 'data_plexus' 
                        AND table_name = '{table}'
                    """))
                    
                    if result.fetchone()[0] > 0:
                        existing_tables.append(table)
                    else:
                        missing_tables.append(table)
                
                success = len(missing_tables) == 0
                
                return {
                    'success': success,
                    'existing_tables': existing_tables,
                    'missing_tables': missing_tables,
                    'test_type': 'database_setup'
                }
                
        except Exception as e:
            logger.error(f"数据库设置验证失败: {str(e)}")
            return {
                'success': False,
                'error': str(e),
                'test_type': 'database_setup'
            }
    
    def check_frontend_files(self) -> Dict[str, Any]:
        """检查前端文件是否存在"""
        logger.info("检查前端文件...")
        
        try:
            frontend_files = [
                'frontend/src/types/datasource.ts',
                'frontend/src/services/datasourceApi.ts',
                'frontend/src/components/DataSourceForm.tsx',
                'frontend/src/components/DataSourceTestModal.tsx',
                'frontend/src/pages/DataSourceManagement.tsx',
                'frontend/src/tests/datasourceIntegrationTest.ts',
                'frontend/src/tests/themeAdaptationTest.ts',
                'frontend/src/tests/comprehensiveTestRunner.ts'
            ]
            
            existing_files = []
            missing_files = []
            
            for file_path in frontend_files:
                full_path = os.path.join(self.project_root, file_path)
                if os.path.exists(full_path):
                    existing_files.append(file_path)
                else:
                    missing_files.append(file_path)
            
            success = len(missing_files) == 0
            
            return {
                'success': success,
                'existing_files': existing_files,
                'missing_files': missing_files,
                'test_type': 'frontend_files'
            }
            
        except Exception as e:
            logger.error(f"前端文件检查失败: {str(e)}")
            return {
                'success': False,
                'error': str(e),
                'test_type': 'frontend_files'
            }
    
    def check_backend_files(self) -> Dict[str, Any]:
        """检查后端文件是否存在"""
        logger.info("检查后端文件...")
        
        try:
            backend_files = [
                'backend/app/models/datasource.py',
                'backend/app/schemas/datasource.py',
                'backend/app/services/datasource_service.py',
                'backend/app/api/v1/endpoints/datasources.py',
                'backend/app/utils/encryption.py',
                'backend/app/utils/database_connector.py'
            ]
            
            existing_files = []
            missing_files = []
            
            for file_path in backend_files:
                full_path = os.path.join(self.project_root, file_path)
                if os.path.exists(full_path):
                    existing_files.append(file_path)
                else:
                    missing_files.append(file_path)
            
            success = len(missing_files) == 0
            
            return {
                'success': success,
                'existing_files': existing_files,
                'missing_files': missing_files,
                'test_type': 'backend_files'
            }
            
        except Exception as e:
            logger.error(f"后端文件检查失败: {str(e)}")
            return {
                'success': False,
                'error': str(e),
                'test_type': 'backend_files'
            }
    
    def check_script_files(self) -> Dict[str, Any]:
        """检查脚本文件是否存在"""
        logger.info("检查脚本文件...")
        
        try:
            script_files = [
                'scripts/create_datasource_tables.sql',
                'scripts/init_datasource_permissions.py',
                'scripts/add_datasource_route.py',
                'scripts/setup_datasource_management.py',
                'scripts/test_datasource_permissions.py',
                'scripts/install_datasource_dependencies.py'
            ]
            
            existing_files = []
            missing_files = []
            
            for file_path in script_files:
                full_path = os.path.join(self.project_root, file_path)
                if os.path.exists(full_path):
                    existing_files.append(file_path)
                else:
                    missing_files.append(file_path)
            
            success = len(missing_files) == 0
            
            return {
                'success': success,
                'existing_files': existing_files,
                'missing_files': missing_files,
                'test_type': 'script_files'
            }
            
        except Exception as e:
            logger.error(f"脚本文件检查失败: {str(e)}")
            return {
                'success': False,
                'error': str(e),
                'test_type': 'script_files'
            }
    
    async def run_all_tests(self) -> Dict[str, Any]:
        """运行所有集成测试"""
        logger.info("开始运行最终集成验证...")
        
        start_time = datetime.now()
        
        # 文件存在性检查
        logger.info("第1阶段：文件存在性检查")
        self.test_results['backend_files'] = self.check_backend_files()
        self.test_results['frontend_files'] = self.check_frontend_files()
        self.test_results['script_files'] = self.check_script_files()
        
        # 数据库设置验证
        logger.info("第2阶段：数据库设置验证")
        self.test_results['database_setup'] = self.run_database_setup_verification()
        
        # 后端集成验证
        logger.info("第3阶段：后端集成验证")
        self.test_results['backend_integration'] = await self.run_backend_verification()
        
        # 性能验证
        logger.info("第4阶段：性能验证")
        self.test_results['performance'] = await self.run_performance_verification()
        
        end_time = datetime.now()
        duration = (end_time - start_time).total_seconds()
        
        # 计算总体成功状态
        overall_success = all(
            result['success'] for result in self.test_results.values()
        )
        
        return {
            'overall_success': overall_success,
            'start_time': start_time,
            'end_time': end_time,
            'duration': duration,
            'test_results': self.test_results
        }
    
    def generate_final_report(self, results: Dict[str, Any]) -> str:
        """生成最终报告"""
        timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        
        report = f"""
{'='*100}
🎯 DataPlexus 数据源管理功能最终集成验证报告
{'='*100}
生成时间: {timestamp}
测试耗时: {results['duration']:.2f}秒
总体状态: {'✅ 验证通过' if results['overall_success'] else '❌ 验证失败'}

📋 验证项目详情:
{'-'*50}
"""
        
        # 文件检查结果
        for test_name, test_result in results['test_results'].items():
            status = '✅ 通过' if test_result['success'] else '❌ 失败'
            report += f"\n{self.get_test_display_name(test_name)}: {status}\n"
            
            if not test_result['success']:
                if 'error' in test_result:
                    report += f"  错误: {test_result['error']}\n"
                if 'missing_files' in test_result and test_result['missing_files']:
                    report += f"  缺失文件: {', '.join(test_result['missing_files'])}\n"
                if 'missing_tables' in test_result and test_result['missing_tables']:
                    report += f"  缺失表: {', '.join(test_result['missing_tables'])}\n"
        
        # 功能完整性评估
        report += f"\n📊 功能完整性评估:\n"
        report += f"{'-'*50}\n"
        
        passed_tests = sum(1 for result in results['test_results'].values() if result['success'])
        total_tests = len(results['test_results'])
        completion_rate = (passed_tests / total_tests) * 100
        
        report += f"验证项目总数: {total_tests}\n"
        report += f"通过项目数: {passed_tests}\n"
        report += f"完成率: {completion_rate:.1f}%\n"
        
        # 部署建议
        report += f"\n💡 部署建议:\n"
        report += f"{'-'*50}\n"
        
        if results['overall_success']:
            report += "✅ 所有验证项目通过，数据源管理功能已准备就绪\n"
            report += "✅ 可以安全部署到生产环境\n"
            report += "✅ 建议进行用户验收测试\n"
        else:
            report += "⚠️ 存在验证失败项目，需要修复后再部署\n"
            report += "⚠️ 请检查失败项目的详细错误信息\n"
            report += "⚠️ 修复问题后重新运行验证\n"
        
        # 后续维护建议
        report += f"\n🔧 后续维护建议:\n"
        report += f"{'-'*50}\n"
        report += "1. 定期运行集成验证脚本确保功能稳定\n"
        report += "2. 监控数据源连接性能和成功率\n"
        report += "3. 定期备份数据源配置数据\n"
        report += "4. 关注用户反馈并持续优化用户体验\n"
        report += "5. 保持依赖包的安全更新\n"
        
        report += f"\n{'='*100}\n"
        report += "报告结束\n"
        report += f"{'='*100}\n"
        
        return report
    
    def get_test_display_name(self, test_name: str) -> str:
        """获取测试显示名称"""
        display_names = {
            'backend_files': '📁 后端文件检查',
            'frontend_files': '📁 前端文件检查',
            'script_files': '📁 脚本文件检查',
            'database_setup': '🗄️ 数据库设置验证',
            'backend_integration': '🔧 后端集成验证',
            'performance': '⚡ 性能验证'
        }
        return display_names.get(test_name, test_name)


def print_summary(results: Dict[str, Any]):
    """打印测试摘要"""
    print("\n" + "="*80)
    print("🎯 DataPlexus 数据源管理最终集成验证摘要")
    print("="*80)
    
    for test_name, test_result in results['test_results'].items():
        status = '✅' if test_result['success'] else '❌'
        display_name = FinalIntegrationTest().get_test_display_name(test_name)
        print(f"{status} {display_name}")
    
    print(f"\n总体状态: {'✅ 验证通过' if results['overall_success'] else '❌ 验证失败'}")
    print(f"测试耗时: {results['duration']:.2f}秒")
    print("="*80)


async def main():
    """主函数"""
    try:
        tester = FinalIntegrationTest()
        results = await tester.run_all_tests()
        
        # 打印摘要
        print_summary(results)
        
        # 生成详细报告
        report = tester.generate_final_report(results)
        
        # 保存报告到文件
        report_file = os.path.join(
            tester.script_dir, 
            f"datasource_integration_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.txt"
        )
        
        with open(report_file, 'w', encoding='utf-8') as f:
            f.write(report)
        
        print(f"\n📄 详细报告已保存到: {report_file}")
        
        return results['overall_success']
        
    except Exception as e:
        print(f"\n❌ 最终集成验证失败: {str(e)}")
        return False


if __name__ == "__main__":
    success = asyncio.run(main())
    sys.exit(0 if success else 1)
